package com.baomidou.springwind.controller.supply;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.kisso.annotation.Permission;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.springwind.controller.BaseController;
import com.baomidou.springwind.entity.NetworkDelivery;
import com.baomidou.springwind.entity.NetworkDepotGoods;
import com.baomidou.springwind.entity.Project;
import com.baomidou.springwind.mapper.NetworkDeliveryDao;
import com.baomidou.springwind.service.DepotGoodsService;
import com.baomidou.springwind.service.NetworkDepotService;
import com.baomidou.springwind.service.ProjectService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 物流供应链
 * </p>
 *
 * @Author huangtao
 * @Date 2017-9-12
 */
@Controller
@RequestMapping("/network/supply")
public class DepotGoodsController extends BaseController {


    @Autowired
    private DepotGoodsService depotGoodsService;
    @Autowired
    private ProjectService projectService;
    @Autowired
    private NetworkDeliveryDao networkDeliveryDao;

    @RequestMapping("/depotGoods/list")
    @Permission("1001")
    public String deliveryList(Model model) {
        EntityWrapper<Project> wrapper = new EntityWrapper<Project>();
        wrapper.eq("operator_id",getCurrentUserId());
        wrapper.in("state", Arrays.asList("1", "2"));
        model.addAttribute("prjList", projectService.selectList(wrapper));
        return pjaxPath("/file/depot_goods_list", model);
    }
    /**
     * 数据导入--仓库商品单位成本
     */
    @ResponseBody
    @RequestMapping("/depotGoods/getData")
    @Permission("1001")
    public String getData(Page<NetworkDepotGoods> page) {
        String batchId=request.getParameter("batchId");
        page = new Page<NetworkDepotGoods>(page.getCurrent(), page.getSize());
        EntityWrapper<NetworkDepotGoods> wrapper = new EntityWrapper<NetworkDepotGoods>();
        if (StringUtils.isNotEmpty(batchId)) {

        }
        if (batchId==null){
            batchId=getProjectId();
            if(batchId.length()==0){
                batchId="-1";
            }
        }
        wrapper.eq("batch_id", batchId);
        wrapper.eq("operator_id",getCurrentUserId());
        page = depotGoodsService.selectPage(page, wrapper);
        List<NetworkDepotGoods> networkDeliveryList = page.getRecords();
        if (networkDeliveryList.size() > 0) {
            Project project = projectService.selectById(networkDeliveryList.get(0).getBatchId());
            for (NetworkDepotGoods networkDelivery : networkDeliveryList) {
                networkDelivery.setBatchName(project == null ? "--" : project.getName());
            }
        }
        return jsonPage(page);
    }
}
